package com.b2c.web;

import com.b2c.service.SpecificationService;


import com.mr.service.pojo.SpecGroup;
import com.mr.service.pojo.SpecParam;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName SpecificationController
 * @Description: TODO
 * @Author kun
 * @Date 2020/7/25
 * @Version V1.0
 **/
@RestController
@RequestMapping("spec")
public class SpecificationController {

    @Autowired
    private SpecificationService specificationService;

    @GetMapping("groups/{cid}")
    public ResponseEntity<List<SpecGroup>> querySpecGroupList(@PathVariable("cid") Long cid){

        return ResponseEntity.ok(specificationService.querySpecGroups(cid));
    }

    @GetMapping("/params")
    public ResponseEntity<List<SpecParam>> querySpecParam(
            @RequestParam(value="gid", required = false) Long gid,
            @RequestParam(value="cid", required = false) Long cid,
            @RequestParam(value="searching", required = false) Boolean searching,
            @RequestParam(value="generic", required = false) Boolean generic
            ){
        List<SpecParam> list =
                this.specificationService.querySpecParams(gid,cid,searching,generic);
        if(list == null || list.size() == 0){
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return ResponseEntity.ok(list);
    }

    @PostMapping("group")
    public ResponseEntity<Void> addSpec(@RequestBody SpecGroup entity){
        specificationService.save(entity);

        return ResponseEntity.ok(null);
    }

    @PostMapping("param")
    public ResponseEntity<Void> save(@RequestBody SpecParam entity){
        specificationService.add(entity);

        return ResponseEntity.ok(null);
    }


    @DeleteMapping("group/{id}")
    public  ResponseEntity deleteSpec(@PathVariable("id") Long id){

        specificationService.deleteSpec(id);

        return ResponseEntity.ok(null);
    }

    @DeleteMapping("param/{id}")
    public  ResponseEntity delete(@PathVariable("id") Long id){

        specificationService.delete(id);

        return ResponseEntity.ok(null);
    }

    @PutMapping("group")
    public ResponseEntity<Void> updataSpec(@RequestBody SpecGroup entity){
        specificationService.save(entity);

        return ResponseEntity.ok(null);
    }

    @PutMapping("param")
    public ResponseEntity<Void> Updata(@RequestBody SpecParam entity){
        specificationService.add(entity);

        return ResponseEntity.ok(null);
    }
}
